package net.exoweb.animate.graphics
{
    import net.exoweb.animate.common.Maths;
    import net.exoweb.animate.graphics.GShape;

    public class GSuperEllipse extends GShape
    {
        protected var _rx:Number;
        protected var _ry:Number;
        protected var _factor:Number;
        protected var _border:Number;
        protected var _borderColor:uint;
        protected var _borderAlpha:Number;

        public function GSuperEllipse(rx:Number, ry:Number, factor:Number=1.0, color:uint=0xFFFFFF, border:Number=NaN, borderColor:uint=0xFFFFFF, borderAlpha:Number=1, bm:String="normal")
        {
            super(color, bm);
            _rx = rx;
            _ry = ry;
            _factor = factor;
            _border = border;
            _borderColor = borderColor;
            _borderAlpha = borderAlpha;
        }

        public function set rx(v:Number):void
        {
            _rx = v;
            display();
        }

        public function get rx():Number
        {
            return _rx;
        }

        public function set ry(v:Number):void
        {
            _ry = v;
            display();
        }

        public function get ry():Number
        {
            return _ry;
        }

        public function set border(v:Number):void
        {
            _border = v;
            display();
        }

        public function get border():Number
        {
            return _border;
        }

        public function set borderColor(v:uint):void
        {
            _borderColor = v;
            display();
        }

        public function get borderColor():uint
        {
            return _borderColor;
        }

        public function set borderAlpha(v:Number):void
        {
            _borderAlpha = v;
            display();
        }

        public function get borderAlpha():Number
        {
            return _borderAlpha;
        }

        public function set factor(v:Number):void
        {
            _factor = v;
            display();
        }

        public function get factor():Number
        {
            return _factor;
        }

        override public function display():void
        {
            var q:Number = Maths.getRadian(0);
            var dx:Number = _rx * Math.pow(Math.cos(q), _factor);
            var dy:Number = _ry * Math.pow(Math.sin(q), _factor);
            graphics.clear();
            graphics.moveTo(dx, dy);
            graphics.lineStyle(_border, _borderColor, _borderAlpha);
            graphics.beginFill(color, alpha);
            for(var i:int=0; i<90; i++)
            {
                q = Maths.getRadian(i);
                dx = _rx * Math.pow(Math.cos(q), _factor);
                dy = _ry * Math.pow(Math.sin(q), _factor);
                graphics.lineTo(dx, dy);
            }
            for(i=90; i>0; i--)
            {
                q = Maths.getRadian(i);
                dx = -_rx * Math.pow(Math.cos(q), _factor);
                dy = _ry * Math.pow(Math.sin(q), _factor);
                graphics.lineTo(dx, dy);
            }
            for(i=0; i<90; i++)
            {
                q = Maths.getRadian(i);
                dx = -_rx * Math.pow(Math.cos(q), _factor);
                dy = -_ry * Math.pow(Math.sin(q), _factor);
                graphics.lineTo(dx, dy);
            }
            for(i=90; i>0; i--)
            {
                q = Maths.getRadian(i);
                dx = _rx * Math.pow(Math.cos(q), _factor);
                dy = -_ry * Math.pow(Math.sin(q), _factor);
                graphics.lineTo(dx, dy);
            }
            graphics.endFill();
        }
    }
}
